<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />

        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象 相当于是departs-->
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <AddDept
        :showDialog.sync="showDialog"
        :tree-node="node"
        @updateDepts="getDepartments"
        ref="addDept"
      />
    </div>
  </div>
</template>

<script>
import AddDept from "./components/add-dept.vue";
import { findChildren } from "@/utils";
import TreeTools from "./components/tree-tools.vue";
import { getDepartments } from "@/api/departments";
export default {
  components: {
    TreeTools,
    AddDept,
  },
  data() {
    return {
      company: {},
      departs: [],
      // 树形结构的桥梁
      defaultProps: {
        label: "name", // 表示 从这个属性显示内容
      },
      showDialog: false,
      node: null,
    };
  },
  created() {
    // 获取组织架构 渲染页面
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments();
      this.company = { name: result.companyName, manager: "负责人", id: "" };
      this.departs = findChildren(result.depts, "");
      console.log(result);
    },
    addDepts(node) {
      this.showDialog = true;
      this.node = node;
    },
    editDepts(node) {
      this.showDialog = true; // 显示新增组件弹层
      this.node = node; // 存储传递过来的node数据
      console.log(this.node);
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>

